﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Shared.Service.Authorizations.Authorization;
using Shared.Service.Authorizations.Authorization.HttpApis;
using Shared.Service.DataScopes;
using System;
using System.Linq;
using Volo.Abp.Authorization;
using Volo.Abp.Caching;
using Volo.Abp.Modularity;


namespace Shared.Service.Authorizations
{
    [DependsOn(//typeof(SharedDomainModule),
       // typeof(YouLuPermissionModule),
       
        typeof(AbpCachingModule),
        typeof(SharedDataScopeModule))]
    
    public class SharedAuthorizationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var permissionRequirementHandler = context.Services.FirstOrDefault(descriptor =>
                descriptor.ServiceType == typeof(IAuthorizationHandler) &&
                descriptor.ImplementationType == typeof(PermissionRequirementHandler));

            if (permissionRequirementHandler != null)
            {
                context.Services.Remove(permissionRequirementHandler);
            }

            var configuration = context.Services.GetConfiguration();

            Configure<WebApiClientOptions>(configuration.GetSection("WebApiClient"));

            context.Services.AddSingleton<IAuthorizationHandler, CustomPermissionRequirementHandler>();

            context.Services.AddHttpClient<IDataScopesHttpApi, DataScopesHttpApi>()
                .SetHandlerLifetime(TimeSpan.FromHours(1));

        }
    }
}
